#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
use std::mem;
use crate::alloc_testing::*;
extern "C" {
    fn memcpy(
        _: *mut libc::c_void,
        _: *const libc::c_void,
        _: libc::c_ulong,
    ) -> *mut libc::c_void;
            }
pub type size_t = libc::c_ulong;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _BloomFilter {
    pub hash_func: BloomFilterHashFunc,
    pub table: *mut libc::c_uchar,
    pub table_size: libc::c_uint,
    pub num_functions: libc::c_uint,
}
pub type BloomFilterHashFunc = Option::<
    unsafe extern "C" fn(BloomFilterValue) -> libc::c_uint,
>;
pub type BloomFilterValue = *mut libc::c_void;
pub type BloomFilter = _BloomFilter;
static mut salts: [libc::c_uint; 64] = [
    0x1953c322 as libc::c_int as libc::c_uint,
    0x588ccf17 as libc::c_int as libc::c_uint,
    0x64bf600c as libc::c_int as libc::c_uint,
    0xa6be3f3d as libc::c_uint,
    0x341a02ea as libc::c_int as libc::c_uint,
    0x15b03217 as libc::c_int as libc::c_uint,
    0x3b062858 as libc::c_int as libc::c_uint,
    0x5956fd06 as libc::c_int as libc::c_uint,
    0x18b5624f as libc::c_int as libc::c_uint,
    0xe3be0b46 as libc::c_uint,
    0x20ffcd5c as libc::c_int as libc::c_uint,
    0xa35dfd2b as libc::c_uint,
    0x1fc4a9bf as libc::c_int as libc::c_uint,
    0x57c45d5c as libc::c_int as libc::c_uint,
    0xa8661c4a as libc::c_uint,
    0x4f1b74d2 as libc::c_int as libc::c_uint,
    0x5a6dde13 as libc::c_int as libc::c_uint,
    0x3b18dac6 as libc::c_int as libc::c_uint,
    0x5a8afbf as libc::c_int as libc::c_uint,
    0xbbda2fe2 as libc::c_uint,
    0xa2520d78 as libc::c_uint,
    0xe7934849 as libc::c_uint,
    0xd541bc75 as libc::c_uint,
    0x9a55b57 as libc::c_int as libc::c_uint,
    0x9b345ae2 as libc::c_uint,
    0xfc2d26af as libc::c_uint,
    0x38679cef as libc::c_int as libc::c_uint,
    0x81bd1e0d as libc::c_uint,
    0x654681ae as libc::c_int as libc::c_uint,
    0x4b3d87ad as libc::c_int as libc::c_uint,
    0xd5ff10fb as libc::c_uint,
    0x23b32f67 as libc::c_int as libc::c_uint,
    0xafc7e366 as libc::c_uint,
    0xdd955ead as libc::c_uint,
    0xe7c34b1c as libc::c_uint,
    0xfeace0a6 as libc::c_uint,
    0xeb16f09d as libc::c_uint,
    0x3c57a72d as libc::c_int as libc::c_uint,
    0x2c8294c5 as libc::c_int as libc::c_uint,
    0xba92662a as libc::c_uint,
    0xcd5b2d14 as libc::c_uint,
    0x743936c8 as libc::c_int as libc::c_uint,
    0x2489beff as libc::c_int as libc::c_uint,
    0xc6c56e00 as libc::c_uint,
    0x74a4f606 as libc::c_int as libc::c_uint,
    0xb244a94a as libc::c_uint,
    0x5edfc423 as libc::c_int as libc::c_uint,
    0xf1901934 as libc::c_uint,
    0x24af7691 as libc::c_int as libc::c_uint,
    0xf6c98b25 as libc::c_uint,
    0xea25af46 as libc::c_uint,
    0x76d5f2e6 as libc::c_int as libc::c_uint,
    0x5e33cdf2 as libc::c_int as libc::c_uint,
    0x445eb357 as libc::c_int as libc::c_uint,
    0x88556bd2 as libc::c_uint,
    0x70d1da7a as libc::c_int as libc::c_uint,
    0x54449368 as libc::c_int as libc::c_uint,
    0x381020bc as libc::c_int as libc::c_uint,
    0x1c0520bf as libc::c_int as libc::c_uint,
    0xf7e44942 as libc::c_uint,
    0xa27e2a58 as libc::c_uint,
    0x66866fc5 as libc::c_int as libc::c_uint,
    0x12519ce7 as libc::c_int as libc::c_uint,
    0x437a8456 as libc::c_int as libc::c_uint,
];
#[no_mangle]
pub extern "C" fn bloom_filter_new(
    mut table_size: libc::c_uint,
    mut hash_func: BloomFilterHashFunc,
    mut num_functions: libc::c_uint,
) -> *mut BloomFilter {
    if num_functions as libc::c_ulong
        > (std::mem::size_of::<[libc::c_uint; 64]>() as libc::c_ulong)
            .wrapping_div(std::mem::size_of::<libc::c_uint>() as libc::c_ulong)
    {
        return std::ptr::null_mut();
    }

    let filter: *mut BloomFilter = unsafe {
        alloc_test_malloc(std::mem::size_of::<BloomFilter>() as libc::c_ulong) as *mut BloomFilter
    };

    if filter.is_null() {
        return std::ptr::null_mut();
    }

    let mut boxed_filter = unsafe { Box::from_raw(filter) };
    boxed_filter.table = unsafe {
        alloc_test_calloc(
            table_size
                .wrapping_add(7)
                .wrapping_div(8) as size_t,
            1 as size_t,
        ) as *mut libc::c_uchar
    };

    if boxed_filter.table.is_null() {
        alloc_test_free(filter as *mut libc::c_void);
        return std::ptr::null_mut();
    }

    boxed_filter.hash_func = hash_func;
    boxed_filter.num_functions = num_functions;
    boxed_filter.table_size = table_size;

    let filter = Box::into_raw(boxed_filter);
    filter
}
#[no_mangle]
pub extern "C" fn bloom_filter_free(mut bloomfilter: *mut BloomFilter) {
    if !bloomfilter.is_null() {
        let boxed_bloomfilter = unsafe { Box::from_raw(bloomfilter) };
        alloc_test_free(boxed_bloomfilter.table as *mut libc::c_void);
        alloc_test_free(bloomfilter as *mut libc::c_void);
        let bloomfilter = Box::into_raw(boxed_bloomfilter);
    }
}
#[no_mangle]
pub extern "C" fn bloom_filter_insert(
    mut bloomfilter: *mut BloomFilter,
    mut value: BloomFilterValue,
) {
    let mut hash: libc::c_uint = 0;
    let mut subhash: libc::c_uint = 0;
    let mut index: libc::c_uint = 0;
    let mut i: libc::c_uint = 0;
    let mut b: libc::c_uchar = 0;

    let boxed_bloomfilter = unsafe { Box::from_raw(bloomfilter) };

    unsafe {
        hash = (boxed_bloomfilter.hash_func).expect("non-null function pointer")(value);
    }

    i = 0;
    while i < boxed_bloomfilter.num_functions {
        subhash = hash ^ unsafe { *salts.get_unchecked(i as usize) };
        index = subhash.wrapping_rem(boxed_bloomfilter.table_size);
        b = ((1 as libc::c_int) << index.wrapping_rem(8)) as libc::c_uchar;

        unsafe {
            let ref mut fresh0 = *boxed_bloomfilter.table.offset(index.wrapping_div(8) as isize);
            *fresh0 = (*fresh0 as libc::c_int | b as libc::c_int) as libc::c_uchar;
        }

        i = i.wrapping_add(1);
    }

    bloomfilter = Box::into_raw(boxed_bloomfilter);
}
#[no_mangle]
pub extern "C" fn bloom_filter_query(
    mut bloomfilter: *mut BloomFilter,
    mut value: BloomFilterValue,
) -> libc::c_int {
    let mut hash: libc::c_uint = 0;
    let mut subhash: libc::c_uint = 0;
    let mut index: libc::c_uint = 0;
    let mut i: libc::c_uint = 0;
    let mut b: libc::c_uchar = 0;
    let mut bit: libc::c_int = 0;

    let boxed_bloomfilter = unsafe { Box::from_raw(bloomfilter) };

    unsafe {
        hash = (boxed_bloomfilter.hash_func).expect("non-null function pointer")(value);
    }

    i = 0;
    while i < boxed_bloomfilter.num_functions {
        subhash = hash ^ unsafe { *salts.get_unchecked(i as usize) };
        index = subhash.wrapping_rem(boxed_bloomfilter.table_size);

        unsafe {
            b = *boxed_bloomfilter.table.offset(index.wrapping_div(8) as isize);
        }

        bit = (1 as libc::c_int) << index.wrapping_rem(8);
        if b as libc::c_int & bit == 0 {
            bloomfilter = Box::into_raw(boxed_bloomfilter);
            return 0;
        }
        i = i.wrapping_add(1);
    }
    bloomfilter = Box::into_raw(boxed_bloomfilter);
    return 1;
}
#[no_mangle]
pub extern "C" fn bloom_filter_read(
    mut bloomfilter: *mut BloomFilter,
    mut array: *mut libc::c_uchar,
) {
    let mut array_size: libc::c_uint = 0;
    let boxed_bloomfilter = unsafe { Box::from_raw(bloomfilter) };
    array_size = (boxed_bloomfilter.table_size)
        .wrapping_add(7)
        .wrapping_div(8);
    unsafe {
        memcpy(
            array as *mut libc::c_void,
            boxed_bloomfilter.table as *const libc::c_void,
            array_size as libc::c_ulong,
        );
    }
    bloomfilter = Box::into_raw(boxed_bloomfilter);
}
#[no_mangle]
pub extern "C" fn bloom_filter_load(
    mut bloomfilter: *mut BloomFilter,
    mut array: *mut libc::c_uchar,
) {
    let mut array_size: libc::c_uint = 0;
    let boxed_bloomfilter = unsafe { Box::from_raw(bloomfilter) };

    array_size = (boxed_bloomfilter.table_size)
        .wrapping_add(7)
        .wrapping_div(8);

    unsafe {
        memcpy(
            boxed_bloomfilter.table as *mut libc::c_void,
            array as *const libc::c_void,
            array_size as libc::c_ulong,
        );
    }

    bloomfilter = Box::into_raw(boxed_bloomfilter);
}
#[no_mangle]
pub extern "C" fn bloom_filter_union(
    mut filter1: *mut BloomFilter,
    mut filter2: *mut BloomFilter,
) -> *mut BloomFilter {
    let mut result: *mut BloomFilter = std::ptr::null_mut();
    let mut i: libc::c_uint = 0;
    let mut array_size: libc::c_uint = 0;

    let boxed_filter1 = unsafe { Box::from_raw(filter1) };
    let boxed_filter2 = unsafe { Box::from_raw(filter2) };

    if boxed_filter1.table_size != boxed_filter2.table_size
        || boxed_filter1.num_functions != boxed_filter2.num_functions
        || boxed_filter1.hash_func != boxed_filter2.hash_func
    {
        filter1 = Box::into_raw(boxed_filter1);
        filter2 = Box::into_raw(boxed_filter2);
        return std::ptr::null_mut();
    }

    result = bloom_filter_new(
        boxed_filter1.table_size,
        boxed_filter1.hash_func,
        boxed_filter1.num_functions,
    );

    if result.is_null() {
        filter1 = Box::into_raw(boxed_filter1);
        filter2 = Box::into_raw(boxed_filter2);
        return std::ptr::null_mut();
    }

    let mut boxed_result = unsafe { Box::from_raw(result) };
    array_size = (boxed_filter1.table_size).wrapping_add(7).wrapping_div(8);

    while i < array_size {
        unsafe {
            *boxed_result.table.offset(i as isize) =
                (*boxed_filter1.table.offset(i as isize) as libc::c_int
                    | *boxed_filter2.table.offset(i as isize) as libc::c_int) as libc::c_uchar;
        }
        i = i.wrapping_add(1);
    }

    result = Box::into_raw(boxed_result);
    filter1 = Box::into_raw(boxed_filter1);
    filter2 = Box::into_raw(boxed_filter2);
    result
}
#[no_mangle]
pub extern "C" fn bloom_filter_intersection(
    mut filter1: *mut BloomFilter,
    mut filter2: *mut BloomFilter,
) -> *mut BloomFilter {
    let mut result: *mut BloomFilter = std::ptr::null_mut();
    let mut i: libc::c_uint = 0;
    let mut array_size: libc::c_uint = 0;

    let boxed_filter1 = unsafe { Box::from_raw(filter1) };
    let boxed_filter2 = unsafe { Box::from_raw(filter2) };

    if boxed_filter1.table_size != boxed_filter2.table_size
        || boxed_filter1.num_functions != boxed_filter2.num_functions
        || boxed_filter1.hash_func != boxed_filter2.hash_func
    {
        filter1 = Box::into_raw(boxed_filter1);
        filter2 = Box::into_raw(boxed_filter2);
        return std::ptr::null_mut();
    }

    result = bloom_filter_new(
        boxed_filter1.table_size,
        boxed_filter1.hash_func,
        boxed_filter1.num_functions,
    );

    if result.is_null() {
        filter1 = Box::into_raw(boxed_filter1);
        filter2 = Box::into_raw(boxed_filter2);
        return std::ptr::null_mut();
    }

    let mut boxed_result = unsafe { Box::from_raw(result) };
    array_size = (boxed_filter1.table_size)
        .wrapping_add(7)
        .wrapping_div(8);

    while i < array_size {
        unsafe {
            *boxed_result.table.offset(i as isize) =
                (*boxed_filter1.table.offset(i as isize) as libc::c_int
                    & *boxed_filter2.table.offset(i as isize) as libc::c_int) as libc::c_uchar;
        }
        i = i.wrapping_add(1);
    }

    result = Box::into_raw(boxed_result);
    filter1 = Box::into_raw(boxed_filter1);
    filter2 = Box::into_raw(boxed_filter2);
    result
}
